<!DOCTYPE HTML>
<html>

<head>
<title>Register</title>
</head>

<body>
<?php
 
$link = mysqli_connect('localhost', 'root', '') or die("Failed to connect to MySQL: " . mysql_error());
if (mysqli_connect_errno()) {
        echo 'Failed to connect to MySQL';
        exit;
}
$sql = "USE web_db";
$resultado = mysqli_query($link,$sql);
 
function NewUser($link) {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $vorname = $_POST['vorname'];
    $b_date = $_POST['birth_date'];
    $gender = $_POST['gender'];
    $location = $_POST['location'];
    $password =  $_POST['pwd'];
    $admission_date = date('Y-m-d');
    $image = "./images/$id";

    $query = "INSERT INTO staff (id, pwd, vorname, name, birth_date, admission_date, gender, workstation, location, salary, image) VALUES ('$id','$password','$vorname','$name','$b_date','$admission_date','$gender','0', '$location','0','$image')";
    $data = mysqli_query ($link,$query)or die($mysqli->error);
    if($data)
    {
    echo "YOUR REGISTRATION IS COMPLETED...";
    Photo($id);
    }
}

function Register($link) {
    if(!empty($_POST['id']))   //Comprobar 'id' que llega de register.html
    {
        $sql = "SELECT * FROM staff WHERE id = '$_POST[id]' AND pwd = '$_POST[pwd]'";
        $query = mysqli_query($link,$sql) or die($mysqli->error);
 
        if(!$row = mysqli_fetch_array($query) or die($mysqli->error))
        {
            newuser($link);
        }
        else
        {
            echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
        }
    }
}

function Photo($id) {
    $target_path = "images/".$id.".jpg";
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "Image ". $id. ".jpg was uploaded";
    } else{
        echo "There was an error. Try again.";
    }
}


if(isset($_POST['submit']))
{
    Register($link);
}
?>

<p>Would you like to come back?</p>
<a HREF="register.php"><input type="button" value="Register"></a>
<a HREF="index.php"><input type="button" value="Index"></a>
<?php
    header("location:login_page.php");
?>
</body>